package com.hgd.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

import com.hgd.client.device.Device;
import com.hgd.client.device.DeviceImplDelegate;
import com.hgd.client.device.DeviceImplService;
import com.hgd.client.device.Domain;
import com.hgd.client.device.Task;
import com.hgd.client.task.TaskImplDelegate;
import com.hgd.client.task.TaskImplService;
import com.hgd.client.task.TaskKind;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.utility.Execute;

public class DeviceAction extends ActionSupport{
	
	private List<Device> deviceList = new ArrayList<Device>();
	private List<Domain> domainList = new ArrayList<Domain>();
	private List<Task> taskList = new ArrayList<Task>();
	private List<TaskKind> kindList = new ArrayList<TaskKind>();
	private Device device = new Device();
	private int  dBid;
	private int deviceId;
	private int dmId;

	DeviceImplService deviceImplService = new DeviceImplService();
	DeviceImplDelegate deviceImplDelegate = deviceImplService.getDeviceImplPort();
	
	TaskImplService taskImplService = new TaskImplService();
	TaskImplDelegate taskImplDelegate = taskImplService.getTaskImplPort();
	
	ActionContext context = ActionContext.getContext(); 
	Map params = context.getParameters(); 
	
	/**
	 * 锟斤拷取锟斤拷应锟斤拷锟斤拷站锟斤拷锟斤拷锟叫癸拷锟斤拷锟斤拷
	 * @return
	 * @throws Exception
	 */
	public String getAllDomain() throws Exception{
		params = context.getParameters(); 
		String[] id=(String[])params.get("MSId");
		//System.out.println(id[0]);
		int MSId = Integer.parseInt(id[0]);
		domainList = deviceImplDelegate.getAllDomain(MSId);
		return SUCCESS;
	}
	/**
	 * 锟斤拷莨锟斤拷锟秸撅拷锟饺★拷璞�
	 * @return
	 * @throws Exception
	 */
	public String getDeviceByDomain() throws Exception{
		 params = context.getParameters(); 
		 String[] id=(String[])params.get("dmId");
		 int dmid=0;
		 if (id==null) {
			  dmid = dmId;
		}else{
			dmid = Integer.parseInt(id[0]);
		}
		System.out.println(dmid);
		deviceList = deviceImplDelegate.getDeviceInfo(dmid);		
		return SUCCESS;
	}
	/**
	 * 锟斤拷取锟借备锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
	 * @return
	 * @throws Exception
	 */
	public String deviceOperate() throws Exception{
		 params = context.getParameters(); 
		 String[] id=(String[])params.get("dId");
		 int dId=0;
		 if (id==null) {
			  dId = deviceId;
		}else{
			dId = Integer.parseInt(id[0]);
		}
		 device = deviceImplDelegate.getDeviceByDid(dId);
		 taskList = deviceImplDelegate.getTaskByDid(dId);
		 List<Task> tempList = new ArrayList<Task>();
		 for (Iterator iterator = taskList.iterator(); iterator.hasNext();) {
			Task task = (Task) iterator.next();
			if (task.getIsDelete()==1) {
				tempList.add(task);
			}
		}
		 taskList.removeAll(tempList);
		 kindList = taskImplDelegate.getTaskKind(dId);
		return SUCCESS;
	}
	
	/**
	 * 删除设备
	 * @return
	 */
	public String deviceDel(){
		int flag = 0;
		String delResult;
		flag = deviceImplDelegate.deviceDel(deviceId);
		if (flag!=0) {
			dmId = flag;
			delResult = SUCCESS;
		}else{
			delResult = ERROR;
		}
		return delResult;
	}
	public String deviceAdd(){
		System.out.println(device.getDIp()+dmId);
		String addResult;
		int id = deviceImplDelegate.deviceAdd(device, dmId);
		if (id!=0) {
			dmId = id;
			addResult = SUCCESS;
		}else {
			addResult = ERROR;
		}
		return addResult;
	}
	public String deviceTran(){
		System.out.println(dmId);	
		return SUCCESS;
	}
	
	
	public List<TaskKind> getKindList() {
		return kindList;
	}

	public void setKindList(List<TaskKind> kindList) {
		this.kindList = kindList;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}

	public int getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(int deviceId) {
		this.deviceId = deviceId;
	}

	public int getdBid() {
		return dBid;
	}

	public void setdBid(int dBid) {
		this.dBid = dBid;
	}

	public List<Device> getDeviceList() {
		return deviceList;
	}

	public void setDeviceList(List<Device> deviceList) {
		this.deviceList = deviceList;
	}
	
	public List<Domain> getDomainList() {
		return domainList;
	}

	public void setDomainList(List<Domain> domainList) {
		this.domainList = domainList;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}
	
	public int getDmId() {
		return dmId;
	}
	public void setDmId(int dmId) {
		this.dmId = dmId;
	}
}
